#include<iostream>
using namespace std;
//可以通过移位实现a/b
//设a=b*k，k可用二进制表示，所以可以表示为二次幂的和
//a=b*(2^t1+2^t2+…+2^tn)
//所以b*2^t1<=a<b*2^(t1+1),
//令s1=b*(2^t1),a2=a-s1=b*(2^t2+…+2^tn)
//从而可以迭代至an–sn=0

int my_div_2(int a, int b)
{
    int x, y;
    int ans = 0;
    int sig = 0;
    if ((a ^ b) < 0)
    {
        sig = 1;
        a = abs(a);
        b = abs(b);
    }
    while(a >= b){
        x = b;
        y = 1;
        while(a >= (x<<1)){
            x <<= 1;
            y <<= 1;
        }
        a -= x;
        ans += y;
    }
    if (sig == 1) return -ans;
    return ans;
}

//递归实现
int recurse_div(int a, int b){
    if(a < b)
        return 0;
    int x = b;
    int y = 1;
    while(a >= (x<<1)){
        x <<= 1;
        y <<= 1;
    }
    return recurse_div(a - x,b) + y;
}

int my_div_1(int a, int b)
{
    int sig = 0;
    if ((a ^ b) < 0)
    {
        sig = 1;
        a = abs(a);
        b = abs(b);
    }
    int tmp = recurse_div(a, b);
    if (sig == 1) return -tmp;
    else return tmp;
}



int main()
{
    int a = -50;
    int b = 3;
    cout << my_div_1(a, b) << endl;
    return 0;
}
